<template>
  <div class="data-table-container">
    <div class="table-container">
      <el-table :data="tableData" border style="width: 100%" v-loading="loading">
        <el-table-column prop="activityName" label="活动名称" min-width="150" />
        <el-table-column prop="state" label="活动状态" width="120">
          <template #default="scope">
            <el-tag v-if="scope.row.state === 0">报名未开始</el-tag>
            <el-tag type="warning" v-else-if="scope.row.state === 1">报名进行中</el-tag>
            <el-tag type="info" v-else-if="scope.row.state === 2">活动待开始</el-tag>
            <el-tag type="success" v-else-if="scope.row.state === 3">活动进行中</el-tag>
            <el-tag type="danger" v-else-if="scope.row.state === 4">活动已结束</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="discountMode" label="营销方式" width="100">
          <template #default="scope">
            <span v-if="scope.row.discountMode === 1">满减</span>
            <span v-else-if="scope.row.discountMode === 2">优惠券</span>
          </template>
        </el-table-column>
        <el-table-column prop="shopNumber" label="商家数" width="80" />
        <el-table-column prop="productNumber" label="商品数量" width="100" />
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="scope">
            <el-button type="primary" link @click="handleView(scope.row)">查看</el-button>
            <el-button
              v-if="scope.row.state !== 4"
              type="primary"
              link
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              v-if="scope.row.state !== 4"
              type="warning"
              link
              @click="handleEnd(scope.row)"
              >结束</el-button
            >
            <el-button
              v-if="scope.row.state === 4"
              type="danger"
              link
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-wrapper">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref, watch } from 'vue'

  // 定义props
  const props = defineProps({
    tableData: {
      type: Array,
      default: () => []
    },
    loading: {
      type: Boolean,
      default: false
    },
    total: {
      type: Number,
      default: 0
    }
  })

  // 定义emits
  const emit = defineEmits(['view', 'edit', 'end', 'delete', 'size-change', 'current-change'])

  // 分页相关
  const currentPage = ref(1)
  const pageSize = ref(10)

  // 处理查看事件
  const handleView = (row: any) => {
    emit('view', row)
  }

  // 处理编辑事件
  const handleEdit = (row: any) => {
    emit('edit', row)
  }

  // 处理结束活动事件
  const handleEnd = (row: any) => {
    emit('end', row)
  }

  // 处理删除事件
  const handleDelete = (row: any) => {
    emit('delete', row)
  }

  // 处理分页大小变化
  const handleSizeChange = (val: number) => {
    pageSize.value = val
    emit('size-change', val)
  }

  // 处理当前页变化
  const handleCurrentChange = (val: number) => {
    currentPage.value = val
    emit('current-change', val)
  }

  // 监听total变化，重置当前页
  watch(
    () => props.total,
    (newVal, oldVal) => {
      if (newVal !== oldVal) {
        currentPage.value = 1
      }
    }
  )
</script>

<style scoped lang="scss">
  .data-table-container {
    .table-container {
      overflow-x: auto;
      overflow-y: auto;
      touch-action: pan-y;
      -webkit-overflow-scrolling: touch;
      margin-bottom: 20px;
    }

    .pagination-wrapper {
      margin-top: 20px;
      display: flex;
      overflow-x: auto;
      overflow-y: hidden;
      touch-action: pan-x;
      -webkit-overflow-scrolling: touch;
      padding: 10px 0;
      justify-content: flex-start;

      ::v-deep .el-pagination {
        display: flex;
        justify-content: flex-start;
        min-width: auto !important;
        margin: 0 !important;
        padding: 0 10px !important;
      }

      ::v-deep .el-pagination__sizes {
        margin-right: 10px;
      }

      ::v-deep .el-pagination__total {
        margin-right: 10px;
      }

      ::v-deep .el-pagination__jump {
        margin-left: 10px;
      }
    }
  }
</style>
